#include "Including.h"


using namespace std;

namespace MD2Models
{

	int MyMD2Models::ReadMD2Model(const char *filename, struct md2_model_t *mdl)
	{
		FILE *fp;
		int i;

		fp = fopen (filename, "rb");
		if (!fp)
		{
			fprintf (stderr, "Fail to open: \"%s\"!\n", filename);
			return 0;
		}

		// Read header
		fread (&mdl->header, 1, sizeof (struct md2_header_t), fp);

		if ((mdl->header.ident != 844121161) || (mdl->header.version != 8))
		{
			fprintf (stderr, "Incorrect File! \n");
			fclose (fp);
			return 0;
		}
	
		// Memory allocations 
		mdl->skins = (struct md2_skin_t *) malloc (sizeof (struct md2_skin_t) * mdl->header.num_skins);
		mdl->texcoords = (struct md2_texCoord_t *) malloc (sizeof (struct md2_texCoord_t) * mdl->header.num_st);
		mdl->triangles = (struct md2_triangle_t *) malloc (sizeof (struct md2_triangle_t) * mdl->header.num_tris);
		mdl->frames = (struct md2_frame_t *) malloc (sizeof (struct md2_frame_t) * mdl->header.num_frames);
		mdl->glcmds = (int *) malloc (sizeof (int) * mdl->header.num_glcmds);

		// Read model data
		fseek (fp, mdl->header.offset_skins, SEEK_SET);
		fread (mdl->skins, sizeof (struct md2_skin_t), mdl->header.num_skins, fp);
		
		fseek (fp, mdl->header.offset_st, SEEK_SET);
		fread (mdl->texcoords, sizeof (struct md2_texCoord_t), mdl->header.num_st, fp);

		fseek (fp, mdl->header.offset_tris, SEEK_SET);
		fread (mdl->triangles, sizeof (struct md2_triangle_t), mdl->header.num_tris, fp);
		
		fseek (fp, mdl->header.offset_glcmds, SEEK_SET);
		fread (mdl->glcmds, sizeof (int), mdl->header.num_glcmds, fp);
		
		// Read frames 
		fseek (fp, mdl->header.offset_frames, SEEK_SET);
		for (i = 0; i < mdl->header.num_frames; ++i)
		{
			// Memory allocation for vertices of this frame 
			mdl->frames[i].verts = (struct md2_vertex_t *) malloc (sizeof (struct md2_vertex_t) * mdl->header.num_vertices);
		
			// Read frame data 
			fread (mdl->frames[i].scale, sizeof (vec3_t), 1, fp);
			fread (mdl->frames[i].translate, sizeof (vec3_t), 1, fp);
			fread (mdl->frames[i].name, sizeof (char), 16, fp);
			fread (mdl->frames[i].verts, sizeof (struct md2_vertex_t), mdl->header.num_vertices, fp);
		}
		fclose (fp);
		return 1;
	}


	/**
	* Free resources allocated for the model
	 */
	void MyMD2Models::FreeMD2Model (struct md2_model_t *mdl)
	{
	  int i;
	
	  if (mdl->skins)
	    {
	      free (mdl->skins);
	      mdl->skins = NULL;
	    }
	
	  if (mdl->texcoords)
	    {
	      free (mdl->texcoords);
	      mdl->texcoords = NULL;
	    }
	
	  if (mdl->triangles)
	    {
	      free (mdl->triangles);
	      mdl->triangles = NULL;
	    }
	
	  if (mdl->glcmds)
	    {
	      free (mdl->glcmds);
	      mdl->glcmds = NULL;
	    }
	
	  if (mdl->frames)
	    {
	      for (i = 0; i < mdl->header.num_frames; ++i)
		{
		  free (mdl->frames[i].verts);
		  mdl->frames[i].verts = NULL;
		}
	
	      free (mdl->frames);
	      mdl->frames = NULL;
	    }
	}


}